using System;
using System.Data;
using FrameworkX.ObjectSpace.Data.Schema;

namespace FrameworkX.ObjectSpace.Data.Schema
{
	/// <summary>
	/// This is the defautl data type attribute associated 
	/// with a class. The CreateStorage actually creates a 
	/// database table with default data types and lengths
	/// for a property. 
	/// If someone wants their column width
	/// changed or wants to use char instead of varchar then
	/// he could declare the sql declaration Like this
	/// <code>
	///		[DataType("CHAR (100)"]
	/// </code>
	/// </summary>
	[AttributeUsage(AttributeTargets.Property)]
	public class DataTypeAttribute : Attribute, ISqlDataType 
	{
		private string _sqlTypeString = "";
        DbType _dataType = DbType.Object;
        int _length = 0;
        object _defaultValue = null;
        int _precision = 0;
        int _scale = 0;
        bool _isIdentity = false;
        bool _isNullable = true;
        bool _isPrimary = false;
        bool _isUnique = false;
		
		public DataTypeAttribute(string sqlTypeString) 
		{
			_sqlTypeString = sqlTypeString;			
		}

        internal DataTypeAttribute(System.Data.DbType dataType, int length, int precision,
            int scale, bool isNullable, object defaultValue, bool isIdentity, bool isPrimary, bool unique )
        {
            _dataType = dataType;
            _length = length;
            _precision = precision;
            _scale = scale;
            _isNullable = isNullable;
            _defaultValue = defaultValue;
            _isIdentity = isIdentity;
            _isPrimary = isPrimary;
            _isUnique = unique;
        }

        internal DataTypeAttribute(System.Data.DbType dataType, int length, int precision,
            int scale, bool isNullable, object defaultValue, bool isIdentity, bool isPrimary)
        {
            _dataType = dataType;
            _length = length;
            _precision = precision;
            _scale = scale;
            _isNullable = isNullable;
            _defaultValue = defaultValue;
            _isIdentity = isIdentity;
            _isPrimary = isPrimary;
            _isUnique = false;
        }


        public int Length
        {
            get { return _length; }
        }

        public int Precision
        {
            get { return _precision; }
        }

        public int Scale
        {
            get { return _scale; }
        }

        public object DefaultValue
        {
            get { return _defaultValue;}
            set { _defaultValue = value; }
        }

        public bool IsIdentity
        {
            get { return _isIdentity; }
        }

        public bool IsNullable
        {
            get { return _isNullable; }
        }

        public DbType DataType
        {
            get { return _dataType; }
        }

        public bool IsPrimary
        {
            get { return _isPrimary; }
        }

		public string SqlTypeString 
		{
			get
			{
				return _sqlTypeString;
			}
		}
	}
}
